package com.atguigu.yygh.sms.service.impl;

import com.atguigu.yygh.sms.service.SmsService;
import com.atguigu.yygh.sms.utils.HttpUtils;
import com.atguigu.yygh.sms.utils.RandomUtil;
import com.atguigu.yygh.vo.msm.SmsVo;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/7/16
 * @Dersciption
 */
@Service
public class SmsServiceImpl implements SmsService {

    //发送验证码
    @Override
    public Boolean sendCode(String phone, String code) {
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "ec23955c6b4249c0867d8d7f52e5cd99";

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "code:"+code);
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    //预约后发送短信
    @Override
    public Boolean sendMessage(SmsVo smsVo) {
        String phone = smsVo.getPhone();
        String templateCode = smsVo.getTemplateCode();
        Map<String, Object> param = smsVo.getParam();
        //仅为了测试 只发送验证码
        String code = RandomUtil.getSixBitRandom();
        return this.sendCode(phone,code);
    }
}
